#include "Character.h"
#include <iostream>
//This code implements the `Character` class for a game. The constructor sets up basic attributes like name, health, attack, and defense. The class handles damage taking and attacking, and it manages an inventory of items that can boost stats. It also provides methods to display character status and inventory, ensuring a well - structured character representation in the game.
Character::Character(const std::string& _name, const std::string& _desc, 
                     unsigned _health, unsigned _attack, unsigned _defense)
    : name(_name), description(_desc), maxHealth(_health), health(_health),
      attackPower(_attack), defense(_defense) {
}

Character::~Character() {
    // Release memory of all items
    for (Item* item : inventory) {
        delete item;
    }
    inventory.clear();
}

bool Character::isAlive() const {
    return health > 0;
}

bool Character::takeDamage(unsigned damage) {
    // Calculate actual damage, considering defense
    unsigned actualDamage = (damage > defense) ? (damage - defense) : 0;
    
    // Deduct health points
    if (actualDamage >= health) {
        health = 0;
        return false; // Character died
    } else {
        health -= actualDamage;
        return true; // Character still alive
    }
}

bool Character::attack(ICharacter& target) {
    return target.takeDamage(this->attackPower);
}

std::string Character::getName() const {
    return name;
}

std::string Character::getDescription() const {
    return description;
}

unsigned Character::getHealth() const {
    return health;
}

unsigned Character::getMaxHealth() const {
    return maxHealth;
}

unsigned Character::getAttack() const {
    return attackPower;
}

unsigned Character::getDefense() const {
    return defense;
}

std::string Character::toString() const {
    return name + " (HP: " + std::to_string(health) + "/" + std::to_string(maxHealth) + ")";
}

void Character::addToInventory(Item* item) {
    if (item) {
        // Apply item bonuses
        maxHealth += item->healthBonus;
        health += item->healthBonus;
        attackPower += item->attackBonus;
        defense += item->defenseBonus;
        
        // Add item to inventory
        inventory.push_back(item);
        
        std::cout << "You obtained " << item->name << "!" << std::endl;
        if (item->healthBonus > 0)
            std::cout << "Health increased by " << item->healthBonus << "!" << std::endl;
        if (item->attackBonus > 0)
            std::cout << "Attack increased by " << item->attackBonus << "!" << std::endl;
        if (item->defenseBonus > 0)
            std::cout << "Defense increased by " << item->defenseBonus << "!" << std::endl;
    }
}

void Character::displayInventory() const {
    if (inventory.empty()) {
        std::cout << "Your inventory is empty." << std::endl;
        return;
    }
    
    std::cout << "===== INVENTORY =====" << std::endl;
    for (size_t i = 0; i < inventory.size(); ++i) {
        std::cout << (i + 1) << ". " << inventory[i]->toString() << std::endl;
    }
    std::cout << "===================" << std::endl;
}

void Character::display() const {
    std::cout << "Name: " << name << std::endl;
    std::cout << "Description: " << description << std::endl;
    std::cout << "Health: " << health << "/" << maxHealth << std::endl;
    std::cout << "Attack: " << attackPower << std::endl;
    std::cout << "Defense: " << defense << std::endl;
} 